package com.fmyd.qizhuazhua.http;


import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.orhanobut.logger.Logger;

/**
 * by Jove.Nan
 */
public abstract class HttpRequestListener<T> extends AsyncHttpResponseHandler {

    private Class<T> clazz;

    protected HttpRequestListener() {
    }

    public HttpRequestListener(Class<T> clazz) {
        this.clazz = clazz;
    }

    public Class<T> getClazz() {
        return clazz;
    }

    public abstract void onResponse(T response);


    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onSuccess(int i, cz.msebera.android.httpclient.Header[] headers, byte[] bytes) {
        Logger.i("---->" + new String(bytes));
        T result = null;
        try {
            if (clazz != null) result = new Gson().fromJson(new String(bytes), clazz);
            else result = (T) new String(bytes);
        } catch (Exception e) {
            Logger.i("--->" + "Json parse Exception");
            e.printStackTrace();
        }

        if (result != null) onResponse(result);
    }

    @Override
    public void onFailure(int statusCode, cz.msebera.android.httpclient.Header[] headers, byte[] bytes, Throwable error) {
        try {
            Logger.i("error code :" + statusCode);
            Logger.i("error :" + new String(bytes));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            onFailure();
        }
    }


    public void onFailure() {

    }

    @Override
    public void onFinish() {
        super.onFinish();
        dismissLoading();
    }

    private ProgressDialog mDialog;

    public void showLoading(Context context, String msg) {
        showLoadingDialog(context, msg, true);
    }

    public void showLoading(Context context, String msg, boolean cancel) {
        showLoadingDialog(context, msg, cancel);
    }

    public void showLoading(Context context) {
        showLoadingDialog(context, null, true);
    }

    public void showLoadingDialog(Context mContext, String msg, boolean cancel) {
        Activity activity = (Activity) mContext;
        while (activity.getParent() != null) {
            activity = activity.getParent();
        }
        if (mDialog == null) mDialog = new ProgressDialog(activity);

        if (TextUtils.isEmpty(msg)) msg = "正在加载数据";
        mDialog.setMessage(msg);
        mDialog.setCancelable(cancel);


        try {
            mDialog.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 隐藏加载的dialog
     */
    public void dismissLoading() {
        try {
            if (null != mDialog && mDialog.isShowing()) {
                mDialog.dismiss();
                mDialog = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
